package com.rocker.graduation.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import com.rocker.graduation.constants.Constants;
import com.rocker.graduation.entity.CategoryBean;
import com.rocker.graduation.service.ICartService;
import com.rocker.graduation.service.ICategoryService;
import com.rocker.graduation.util.BaseUtils;

@Controller
@RequestMapping("/category")
public class CategoriesController {
	@Autowired
	private ICategoryService categoryService;
	
	@Autowired
	private ICartService cartService;
	
	@RequestMapping("/listCategories.action/{sort}")
	public ModelAndView listCategories(HttpServletRequest request, @PathVariable("sort") String sort){
		ModelAndView mav = new ModelAndView();
		List<CategoryBean> list = null;
		String[] seperate = sort.split("-");
		String page = BaseUtils.getPage(seperate[4]);
		int summary = 0;
		if (!seperate[3].equals("0")) {
			summary = categoryService.getCategoriesNum(seperate[3], "4");
			list = categoryService.listCategoriesByPage(seperate[3], "4", page);
		}else if (!seperate[2].equals("0")) {
			summary = categoryService.getCategoriesNum(seperate[2], "3");
			list = categoryService.listCategoriesByPage(seperate[2], "3", page);
		}else if (!seperate[1].equals("0")) {
			summary = categoryService.getCategoriesNum(seperate[1], "2");
			list = categoryService.listCategoriesByPage(seperate[1], "2", page);
		}else if(!seperate[0].equals("0")){
			summary = categoryService.getCategoriesNum(seperate[0], "1");
			list = categoryService.listCategoriesByPage(seperate[0], "1", page);
		}
		Integer userId = (Integer) WebUtils.getSessionAttribute(request, Constants.USER_ID);
		Integer count = cartService.getItemSummary(userId);
		mav.addObject("itemCount", count);
		mav.addObject("summary", summary);
		mav.addObject("category", list);
		mav.addObject("currentPage", seperate[4]);
		mav.setViewName("/product");
		return mav;
	}
	
	@RequestMapping("/categoryInfo.action/{intemId}")
	public ModelAndView getCategoryInfo(HttpServletRequest request, @PathVariable("intemId") Integer itemId){
		ModelAndView mav = new ModelAndView();
		CategoryBean categoryBean = categoryService.getCategoryInfo(itemId);
		Integer userId = (Integer) WebUtils.getSessionAttribute(request, Constants.USER_ID);
		Integer count = cartService.getItemSummary(userId);
		mav.addObject("itemCount", count);
		mav.addObject("category", categoryBean);
		mav.setViewName("/detail");
		return mav;
	}
	
	@RequestMapping("/home.html")
	public ModelAndView getCategorySales(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		List<CategoryBean> categoryBeans = categoryService.getCategorySales();
		Integer userId = (Integer) WebUtils.getSessionAttribute(request, Constants.USER_ID);
		Integer count = cartService.getItemSummary(userId);
		mav.addObject("itemCount", count);
		mav.addObject("list", categoryBeans);
		mav.setViewName("/home");
		return mav;
	}
	
	@RequestMapping("/search.action")
	public ModelAndView search(HttpServletRequest request, @RequestParam String state, @RequestParam String city){
		ModelAndView mav = new ModelAndView();
		List<CategoryBean> list = null;
		int summary = 0;
		if (!StringUtils.isEmpty(city) && !StringUtils.isEmpty(state)) {
			summary = categoryService.getCategoriesNum(city, "4");
			list = categoryService.getSearchItem(city, "0", "4");
		}else if (StringUtils.isEmpty(city) && !StringUtils.isEmpty(state)) {
			summary = categoryService.getCategoriesNum(state, "3");
			list = categoryService.getSearchItem(state, "0", "3");
		}else {
			return null;
		}
		Integer userId = (Integer) WebUtils.getSessionAttribute(request, Constants.USER_ID);
		Integer count = cartService.getItemSummary(userId);
		mav.addObject("itemCount", count);
		mav.addObject("summary", summary);
		mav.addObject("category", list);
		mav.addObject("currentPage", "1");
		mav.setViewName("/product");
		return mav;
	}
}
